import requests
import re
import os.path
class XbiqugeNovel(object):

    host = "http://www.xbiquge.la"

    book_info_regex = dict(

            book_name = '<meta property="og:novel:book_name" content="(.*?)"/>',
            book_author = '<meta property="og:novel:author" content="(.*?)"/>',
            book_intor = '<meta property="og:description" content="((?s:.*?))"',
            book_chapter = "<dd><a href='(.*?)' >(.*?)</a></dd>",
        )

    chapter_info_regex = dict(

        chapter_name = '<h1>(.*?)</h1>',

        chapter_content = '<div id="content">(.*?)</div>',

        next_chapter_url = '<a href="(/\d+?/\d+?/\d+?.html)">下一章</a>',

        pre_chapter_url = '<a href="(/\d+?/\d+?/\d+?.html)">上一章</a>',
    )

    def get_book_info(self, book_url):#传一个书的链接返回信息
        '''

        book_url: 书名链接

        return: dict

        {
            "book_name": [],
            "book_author": [],
            "book_chapter":{

                "章节名称"："章节链接",
                ... ...
            }
            "status": "",
            "host": "",
            "book_intor": "",
        }

        '''


        Novel_info = requests.get(book_url)

        Novel_info.encoding = Novel_info.apparent_encoding

        html_str = Novel_info.text

        book_info = {}

        book_info['host'] = self.host

        for key,value in self.book_info_regex.items():

            book_info[key] = re.findall(value, html_str)

        book_info['book_intor'] = book_info['book_intor'][0]

        book_info['book_chapter'] = {i[1]: 'http://www.xbiquge.la' +i[0]for i in book_info['book_chapter']}

        return book_info

    def get_chapter_info(self, chapter_url):

        # def get_chapter_info(self, chapter_url):#提取小说章节
        '''
        chapter_url: 章节阅读页面
        return : dict

        {
            "chapter_name": '',
            "chapter_content": "",
            "chapter_url": "",
            "next_chapter_url": "",
            "pre_chapter_url": "",
            "host": "",
        }
        '''

        Novel_info = requests.get(chapter_url)

        Novel_info.encoding = Novel_info.apparent_encoding

        html_str = Novel_info.text

        info = {key: re.findall(value, html_str) for key, value in self.chapter_info_regex.items()}

        print(info)
        if not info['pre_chapter_url']:

            info['pre_chapter_url'] = [chapter_url]
        else:
            info['pre_chapter_url'] =[self.host+ info['pre_chapter_url'][0]]

        if not info['next_chapter_url']:

            info['next_chapter_url'] = [chapter_url]
        else:

            info['next_chapter_url'] =[self.host+ info['next_chapter_url'][0]]




        for i in info:#字典循环返回键

            info[i] = info[i][0]
        
        info['chapter_url'] = chapter_url
        info['host'] = self.host
        info['book_url'] = os.path.dirname(chapter_url)




        return info #bug 如果是首页返回的上一页是全链接，而下一页不是全链接，课后思考！！？
        
    def get_search_info(self, keywords):#搜索书信息

        '''
        keywords: 搜索的关键字
        return:dict
        {
            "keywords": '',
            "host": "",
            "search_info":[
                {'book_name': "", "book_url": "", "book_author": "", "book_status": ""},
                ... ...
            ]

        }
        '''

        url = 'http://www.xbiquge.la/xiaoshuodaquan/'
        r = requests.get(url,headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36'})

        r.encoding = r.apparent_encoding

        html = r.text

        # keywords = "神"

        regex = f'<li><a href="(.*?)">(.*?{keywords}.*?)</a></li>'

        info = re.findall(regex, html)

        search_info = [{'book_name': i[1], "book_url": i[0], "book_author": "", "book_status": ""} for i in info]

        result_info = {
            "keywords" : keywords,
            "host": self.host,
            "search_info": search_info,
        }

        return result_info


if __name__ == '__main__':
    x = XbiqugeNovel()
    # print(x.get_chapter_info('http://www.xbiquge.la/0/419/336634.html'))
    x.get_chapter_info('http://www.xbiquge.la/0/419/336633.html')#第一章节
    # print(x.get_search_info('神'))